package com.yimi.policyservice.controller;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.Page;
import com.yimi.policyservice.common.annotation.Auth;
import com.yimi.policyservice.dto.PageDto;
import com.yimi.policyservice.dto.policyaddr.IdsBean;
import com.yimi.policyservice.dto.role.DataRoleDto;
import com.yimi.policyservice.dto.role.MenuTreeDto;
import com.yimi.policyservice.dto.role.RoleDto;
import com.yimi.policyservice.dto.role.SysRole4Get;
import com.yimi.policyservice.entity.base.system.SysUser;
import com.yimi.policyservice.service.RoleService;
import org.apache.catalina.security.SecurityUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Security;
import java.util.List;

/**
 * @author jack
 */
@RestController
@RequestMapping("role")
public class RoleController  extends  BaseController{

    @Autowired
    private RoleService roleService ;
    @Value("${rootkey}")
    private String rootKey;

    /**
     * 新增角色
     * @param bean
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public SysRole4Get create(@Valid  @RequestBody RoleDto bean){
        return  roleService.create(bean) ;
    }

    /**
     * 修改角色
     * @param id
     * @param bean
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public void update(@PathVariable String id, @RequestBody RoleDto bean){
        roleService.update(bean,id);
    }


    /**
     * 删除角色
     * @param id
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public void del(@PathVariable String id){
        SysRole4Get one = roleService.findOne(id);
        if(one!=null&& StringUtils.isNotBlank(one.getPermissions())){
            if(SecureUtil.md5(rootKey).equals(one.getPermissions())){
                return;
            }
        }
        roleService.delete(id);
    }

    /**
     * 所有角色
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public List<SysRole4Get> get(){
        return  roleService.findAll();
    }

    /**
     * 权限搜索
     * @param key
     * @return
     */
    @RequestMapping(value = "search",method = RequestMethod.GET)
    public PageDto<SysRole4Get> search(@RequestParam(defaultValue = "") String key){
        handlerPage();
        Page<SysRole4Get> res =  roleService.search(key);
        return new PageDto<>(res);
    }

    /**
     * 获取菜单树
     * @return
     */
    @RequestMapping(value = "tree")
    public MenuTreeDto tree(){
        return roleService.getMenuTree() ;
    }

    /**
     * 获取角色已有的菜单权限
     * @return
     */
    @RequestMapping(value = "{id}/menus",method = RequestMethod.GET)
    public IdsBean getRoleMenu(@PathVariable String id){
        return  roleService.getRoleMenuIds(id);
   }


    /**
     * 编辑角色权限
     * @param bean
     */
    @RequestMapping(value = "{id}/permission",method = RequestMethod.POST)
    public void addRolePermission(@PathVariable String id ,@RequestBody IdsBean bean,@RequestParam (defaultValue = "0") int type){
        List<String> ids = bean.getIds();
        roleService.setPermission(id,ids,type);
    }
    @RequestMapping(value = "pages",method = RequestMethod.GET)
    public List<MenuTreeDto> getAllPagePermissions(){
        return roleService.getPagePermisssion();
    }

    @RequestMapping(value = "{id}/pages",method = RequestMethod.GET)
    public IdsBean getRolePages(@PathVariable String id){
        return  roleService.getRolePages(id);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public SysRole4Get findOne(@PathVariable String id){
        return  roleService.findOne(id);
    }


    @RequestMapping(value = "user/data/{userId}",method = RequestMethod.POST)
    @Auth
    public void addUserDataRole(@RequestBody List<DataRoleDto> roles,@PathVariable String userId){
        SysUser sysUser = getLoginUser();
        roleService.addUserDataRole(roles,sysUser,userId);
    }

    @RequestMapping(value = "user/data/{userId}",method = RequestMethod.GET)
    @Auth
    public List<String> getUserDataRole(@PathVariable String userId,@RequestParam(defaultValue = "0") int type){
        return  roleService.getUserDataPermissionByType(userId,type);
    }


}
